/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package servicios.DAO.impl;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import modelo.Cliente;
import modelo.Invoice;
import servicios.DAO.InvoiceDAO;

/**
 *
 * @author epl
 */
public class InvoiceDAOToplink implements InvoiceDAO{

    private EntityManagerFactory emf;
    private EntityManager em;

    public void save(Invoice invoice) {
        em.getTransaction().begin();
        em.persist(invoice);
        em.getTransaction().commit();
    }

    public Invoice get(long id) {
        return (Invoice) em.find(Invoice.class, id);
    }

    public Invoice getByNumero(long numero) {
        Query qt = em.createNamedQuery("findByNumero");
            qt.setParameter("numero", numero);
        List<Invoice> invoices = qt.getResultList();
        return invoices.get(0);
    }

    public List<Invoice> findByFecha(String fecha) {
        Query qt = em.createNamedQuery("findByFecha");
            qt.setParameter("fecha", fecha);
        List<Invoice> invoices = qt.getResultList();
        return invoices;
    }

    public List<Invoice> findByCliente(Cliente cliente) {
        Query qt = em.createNamedQuery("findByCliente");
            qt.setParameter("cliente", cliente);
        List<Invoice> invoices = qt.getResultList();
        return invoices;
    }

    public List<Invoice> findByEstado(Invoice.Status estado) {
        Query qt = em.createNamedQuery("findByEstado");
            qt.setParameter("estado", estado);
        List<Invoice> invoices = qt.getResultList();
        return invoices;
    }

    public void dead(long id) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List<Invoice> getByNumeroList(long numero) {
        Query qt = em.createNamedQuery("findByNumero");
            qt.setParameter("numero", numero);
        List<Invoice> invoices = qt.getResultList();
        return invoices;
    }

    public InvoiceDAOToplink() {
        emf = Persistence.createEntityManagerFactory("Shhh");
        em = emf.createEntityManager();
    }

}
